package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.interfaces.UserDeviceService;
import cn.huiyunche.base.service.mappers.SUserDevicesMapper;
import cn.huiyunche.base.service.model.SUserDevices;
import cn.huiyunche.base.service.model.SUserDevicesExample;
import cn.huiyunche.base.service.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by LYN on 8/4/16.
 */
@Service
public class UserDeviceServiceImpl implements UserDeviceService {

    @Autowired
    private cn.huiyunche.base.service.interfaces.UserService UserService = null;

    @Autowired
    private SUserDevicesMapper userDevicesMapper = null;

    @Override
    public SUserDevices getCurrentUserLastDevice() throws Exception {
        UserVo userVo = getUserService().getCurrentUserVo();
        SUserDevicesExample userDevicesExample = new SUserDevicesExample();
        userDevicesExample.setOrderByClause("id desc");
        userDevicesExample.setLimitStart(0);
        userDevicesExample.setLimitEnd(1);
        userDevicesExample.createCriteria().andUserIdEqualTo(userVo.getId());
        List<SUserDevices> userDevicesList = getUserDevicesMapper().selectByExample(userDevicesExample);
        if (userDevicesList != null && userDevicesList.size() > 0) {
            return userDevicesList.get(0);
        }
        return null;
    }

    @Override
    public SUserDevices getUserLastDevice(Long userid) {
        SUserDevicesExample userDevicesExample = new SUserDevicesExample();
        userDevicesExample.setOrderByClause("id desc");
        userDevicesExample.setLimitStart(0);
        userDevicesExample.setLimitEnd(1);
        userDevicesExample.createCriteria().andUserIdEqualTo(userid);
        List<SUserDevices> userDevicesList = getUserDevicesMapper().selectByExample(userDevicesExample);
        if (userDevicesList != null && userDevicesList.size() > 0) {
            return userDevicesList.get(0);
        }
        return null;
    }

    public cn.huiyunche.base.service.interfaces.UserService getUserService() {
        return UserService;
    }

    public SUserDevicesMapper getUserDevicesMapper() {
        return userDevicesMapper;
    }
}
